
package com.yuke.cloud.service.uac.service.impl;

import com.google.common.base.Preconditions;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.base.dto.GroupInfoDto;
import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.service.api.uac.exceptions.UacBizException;
import com.yuke.cloud.service.uac.dto.LoginRespDto;
import com.yuke.cloud.service.uac.dto.UserGroupDto;
import com.yuke.cloud.service.uac.entity.AccUser;
import com.yuke.cloud.service.uac.security.SecurityUtils;
import com.yuke.cloud.service.uac.service.*;
import com.yuke.cloud.service.uac.vo.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * The class Uac login service.
 *
 * @author
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AccLoginServiceImpl implements AccLoginService {

	@Resource
	private AccUserService accUserService;
	@Resource
	private AccUserGroupService accUserGroupService;
	@Resource
	private AccMenuService accMenuService;
	@Resource
	private AccEmployeeGroupService accEmployeeGroupService;
	@Resource
	private RedisTemplate redisTemplate;



	@Override
	public LoginRespDto loginAfter(Long applicationId) {
		LoginRespDto loginRespDto = new LoginRespDto();
		String loginName = SecurityUtils.getCurrentLoginName();
		if (StringUtils.isEmpty(loginName)) {
			log.error("操作超时, 请重新登录 loginName={}", loginName);
			Preconditions.checkArgument(StringUtils.isNotEmpty(loginName), "操作超时, 请重新登录");
		}

		AccUser accUser = accUserService.findByLoginName(loginName);
		if (PublicUtil.isEmpty(accUser)) {
			log.info("找不到用户信息 loginName={}", loginName);
			throw new UacBizException(ErrorCodeEnum.UAC10011002, loginName);
		}
		UserGroupDto userGroupDto = new UserGroupDto();
		if (accUser.getIsEmployee()){
			userGroupDto = accEmployeeGroupService.selectEmployeeGroupInfoByUserId(accUser.getUserId());
		}else{
			userGroupDto = accUserGroupService.selectUserGroupInfoByUserId(accUser.getUserId());
		}

		if (!PublicUtil.isEmpty(userGroupDto)){
			accUser.setGroupId(userGroupDto.getGroupId());
			accUser.setGroupName(userGroupDto.getGroupName());
			GroupInfoDto groupInfoDto = new GroupInfoDto();
			BeanUtils.copyProperties(userGroupDto,groupInfoDto);
			loginRespDto.setGroupInfoDto(groupInfoDto);
			this.getGroupInfoDto(userGroupDto);
		}
		//获取loginAuthDto
		LoginAuthDto loginAuthDto = this.getLoginAuthDto(accUser);
		List<MenuVo> menuVoList = accMenuService.getOwnMenuListByUserId(accUser.getUserId(), applicationId);
		if (PublicUtil.isNotEmpty(menuVoList) && GlobalConstant.Sys.MENU_ROOT.equals(menuVoList.get(0).getMenuCode())) {
			menuVoList = menuVoList.get(0).getSubMenu();
		}
		loginRespDto.setLoginAuthDto(loginAuthDto);
		loginRespDto.setMenuList(menuVoList);

		return loginRespDto;
	}

	private LoginAuthDto getLoginAuthDto(AccUser accUser) {
		LoginAuthDto loginAuthDto = new LoginAuthDto();
		loginAuthDto.setUserId(accUser.getUserId());
		loginAuthDto.setUserName(accUser.getUserName());
		loginAuthDto.setLoginName(accUser.getLoginName());
		loginAuthDto.setType(accUser.getType());
		loginAuthDto.setGroupId(accUser.getGroupId());
		loginAuthDto.setGroupName(accUser.getGroupName());
		loginAuthDto.setNickname(accUser.getNickname());
		loginAuthDto.setHeadImgUri(accUser.getHeadImgUri());
		return loginAuthDto;
	}

	//todo 20191116 获取group信息
	private void getGroupInfoDto(UserGroupDto userGroupDto) {
		GroupInfoDto groupInfoDto = new GroupInfoDto();
		groupInfoDto.setGroupId(userGroupDto.getGroupId());
		groupInfoDto.setGroupName(userGroupDto.getGroupName());
		groupInfoDto.setLevel(userGroupDto.getLevel());
		groupInfoDto.setParentId(userGroupDto.getParentId());
		redisTemplate.opsForValue().set(GlobalConstant.Sys.ROLE_DEPARTMENT_ADMIN + userGroupDto.getUserId(), groupInfoDto);
	}


}
